<template>
	<view class="container">
		<view class="second">
			<view class="item" @click="mydata">
				<view class="left">
					个人资料
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="item" @click="goSet(1)">
				<view class="left">
					隐私政策
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="item" @click="goSet(2)">
				<view class="left">
					服务协议
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="item" @click="goSet(3)">
				<view class="left">
					关于我们
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view>
<!-- 			<view class="item" @click="goSet(3)">
				<view class="left">
					在线客服
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view> -->
			<!-- #ifdef APP-PLUS -->
			<view class="item" @click="updateversion">
				<view class="left">
					版本更新
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view>
			<!-- #endif -->
			<view class="item" @click="clearstorage">
				<view class="left">
					缓存清除
				</view>
				<view class="right">
					<image src="../../static/xiangyou11.png" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view v-if="loadModal" class="downloadapp">
			<view class="box">
				<view class="title1">版本更新</view>
				<view class="title">新版本下载中...{{progress}}%</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				loadModal: false, //后续下载新版本时，loadModel的状态
				progress: 0, //loadModel中的下载进度
				downloadUrl: '',  //apk下载地址
			}
		},
		onLoad() {
			
		},
		onShow() {

		},
		methods:{
			mydata(){
				uni.navigateTo({
					url:'../person-message/person-message'
				})
			},
			updateversion(){
				uni.showLoading({
					title: '检测新版本中...'
				});
				setTimeout(()=>{
					const BASE_URL = uni.BASE_URL
					uni.request({
						url: BASE_URL+'index/set/base_set',
						method:'GET',
						success:(res) =>{
							uni.hideLoading();
							if(res.data.data.app){
								
								let platform = uni.getSystemInfoSync().platform;
								if (platform == 'android'){
									this.downloadUrl = res.data.data.app.Android_download_url
									var remote_v = res.data.data.app.Android_V;
									var local_v = uni.local_v;
									console.log(remote_v,local_v);
									if(this.compare(remote_v,local_v) == 1){
										uni.showModal({
											title: '温馨提示',
											confirmText:'立即更新',
											cancelText:'暂不更新',
											content: '有新版本V'+remote_v+'，现在更新吗',
											success: (res)=> {
												if (res.confirm) {
													this.loadModal = true;
													const downloadTask = uni.downloadFile({
														url: this.downloadUrl,
														success: (res1) => {
															console.log('下载对象',res1)
															if (res1.statusCode === 200) {
																console.log('下载成功');
																this.loadModal = false
																var filePath = res1.tempFilePath;
																uni.openDocument({
																	filePath: filePath,
																	showMenu: true,
																	success: (res2)=> {
																		console.log('打开应用成功');
																	}
																});
															}
														}
													});
													downloadTask.onProgressUpdate((res) => {
														console.log('下载进度' + res.progress);
														this.progress = parseInt(res.progress)
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
									}else{
										uni.showModal({
										    title: '已是最新版本',
										    content: '当前应用版本号'+local_v,
											showCancel:false,
											success: (res)=> {
												if (res.confirm) {
													console.log('用户点击确定');
												}
											}
										});
									}
								}
							}
						},
						fail:(res)=> {
							console.log(res.data);
						}
					});
				},1000)
			},
			compare(v1, v2) {
				  v1 = v1.split('.')
				  v2 = v2.split('.')
				  const len = Math.max(v1.length, v2.length)

				 // 调整两个版本号位数相同
				  while (v1.length < len) {
					v1.push('0')
				  }
				  while (v2.length < len) {
					v2.push('0')
				  }
				  
				  // 循环判断每位数的大小
				  for (let i = 0; i < len; i++) {
					const num1 = parseInt(v1[i])
					const num2 = parseInt(v2[i])

					if (num1 > num2) {
					  return 1
					} else if (num1 < num2) {
					  return -1
					}
				  }
				  return 0
			},
			clearstorage(){
				uni.showModal({
				    title: '需要清理缓存吗?',
				    content: '清理后本地所有缓存将全部消失',
				    success: function (res) {
				        if (res.confirm) {
							// let base_set = uni.getStorageSync('base_set')
				   //          uni.clearStorage();
							// uni.setStorageSync('base_set',base_set);
							// uni.redirectTo({
							// 	url:'/pages/index/index'
							// })
							uni.removeStorageSync('userinfo');
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			},
			goSet(item){
				console.log(item)
				uni.navigateTo({
					url: '../setabout/setabout?type='+item
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	.container{
		height: 100vh;
		background-color: #ffffff;
		.second{
			.item{
				padding: 0 50upx;
				box-sizing: border-box;
				height: 130upx;
				display: flex;
				justify-content: space-between;
				border-bottom: 2upx solid #eee;
				.left{
					width: 50%;
					height: 100%;
					display: flex;
					align-items: center;
					font-size: 28upx;
					font-weight: 700;
					letter-spacing: 5upx;
					color: #333;
				}
				.right{
					width: 10%;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					image{
						width: 15upx;
						height: 30upx;
					}
				}
			}
		}
		.downloadapp{
			position: fixed;
			z-index: 9999;
			top:0;
			left:0;
			width: 100%;
			height: 100vh;
			background-color: rgba(9, 9, 9, .6);
			display: flex;
			align-items: center;
			justify-content: center;
			.box{
				background-color: #fff;
				width: 70%;
				padding: 30upx;
				border-radius: 30upx;
				.title{
					font-size: 28upx;
					color: #333;
					font-weight: 700;
					letter-spacing: 5upx;
					margin-bottom: 20upx;
				}
				.title1{
					padding: 10upx;
					font-size: 28upx;
					text-align: center;
					font-weight: 700;
					letter-spacing: 5upx;
					margin-bottom: 20upx;
					color: forestgreen;
				}
			}
		}
	}
</style>
